package com.xxl.shoppingapp.base;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Title: BaseRecyclerViewAdapter.java
 * Description:
 * Copyright (c) 版权所有请勿抄袭 2018
 * Created DateTime: 2018/4/12 13:45
 * Created by xuelianx.
 */
public abstract class BaseRecyclerViewAdapter<T extends BaseEntity> extends RecyclerView.Adapter {

    public ItemClickListener mItemClickListener;
    private List<T> data= new ArrayList<>();

    public void setData(List<T> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void addData(List<T> newData) {
        data.addAll(newData);
        notifyDataSetChanged();
    }

    public ItemClickListener getmItemClickListener(){
        return  this.mItemClickListener;
    }

    public List<T> getData() {
        return data;
    }

    public T getItem(int position) {
        return getData().get(position);
    }

    @Override
    public int getItemCount() {
        return getData() == null ? 0 : getData().size();
    }

    /**
     * 创建一个回调接口
     */
    public interface ItemClickListener {
        void onItemClick( int position);
    }

    /**
     * 在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
     *
     * @param myItemClickListener
     */
    public void setItemClickListener(ItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }
}
